package com.ruoyi.project.wx.service.impl;


import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.shop.dao.ShopShopDao;
import com.ruoyi.project.shop.domain.ShopShopDO;
import com.ruoyi.project.wx.dao.ShopExtractDao;
import com.ruoyi.project.wx.domain.ShopExtractDO;
import com.ruoyi.project.wx.service.ShopExtractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ShopExtractServiceImpl implements ShopExtractService {
	@Autowired
	private ShopExtractDao shopExtractDao;
	@Autowired
	private ShopShopDao shopShopDao;
	
	@Override
	public ShopExtractDO get(String id){
		return shopExtractDao.get(id);
	}
	
	@Override
	public List<ShopExtractDO> list(Map<String, Object> map){
		return shopExtractDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return shopExtractDao.count(map);
	}
	
	@Override
	public int save(ShopExtractDO shopExtract){
		return shopExtractDao.save(shopExtract);
	}
	
	@Override
	public int update(ShopExtractDO shopExtract){
		return shopExtractDao.update(shopExtract);
	}
	
	@Override
	public int remove(String id){
		return shopExtractDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return shopExtractDao.batchRemove(ids);
	}

    @Override
    public List<ShopExtractDO> likeList(Map<String, Object> map){
        return shopExtractDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return shopExtractDao.likeListCount(map);
    }

    @Override
    public R shen(ShopExtractDO shopExtract) {


		String id = shopExtract.getId();
		ShopExtractDO extractDO = shopExtractDao.get(id);
		if (extractDO.getStatus() != 0) {
			return R.error("已经提现过了，请刷新页面");
		}
		ShopShopDO shopShopDO = shopShopDao.get(extractDO.getShopId());
		if(shopShopDO.getMoney().compareTo(extractDO.getMoney())<0){
			return R.error("商家余额不够,无法提现");
		}else{
			shopShopDO.setMoney(shopShopDO.getMoney().subtract(extractDO.getMoney()));
			shopShopDao.update(shopShopDO);
		}
		shopExtract.setUpdateAt(new Date());
		shopExtractDao.update(shopExtract);
		return R.ok();
    }

}
